package com.fc.hkx.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fc.hkx.common.ResponseJson;
import com.fc.hkx.dto.CreateOrderRequest;
import com.fc.hkx.dto.PayOrderRequest;
import com.fc.hkx.thirdparty.tongcheng.TongchengApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by futurecreative on 2017/9/8.
 */
@RestController
@RequestMapping("/api")
public class HotelController {

    @Autowired
    private TongchengApiService tongchengApiService;

    @RequestMapping("/hotel/list")
    public ResponseJson list(@RequestParam Map<String, String> params) throws Exception {
        ResponseJson responseJson = new ResponseJson();
        responseJson.setObj(tongchengApiService.getResourceList(params));
        return responseJson;
    }

    @RequestMapping("/hotel/info")
    public ResponseJson info(@RequestParam Map<String, String> params) throws Exception {
        ResponseJson responseJson = new ResponseJson();
        responseJson.setObj(tongchengApiService.getResourceInfo(params));
        return responseJson;
    }

    @RequestMapping("/hotel/saleinfo")
    public ResponseJson saleInfo(@RequestParam Map<String, String> params) throws Exception {
        ResponseJson responseJson = new ResponseJson();
        responseJson.setObj(tongchengApiService.getResSaleInfo(params));
        return responseJson;
    }

    @RequestMapping("/hotel/order")
    public ResponseJson createSingleOrder(@RequestBody CreateOrderRequest createOrderRequest) throws Exception {
        ResponseJson responseJson = new ResponseJson();
        Map<String, String> params = new HashMap<>();
        params.put("ResId", createOrderRequest.getResId().toString());
        params.put("ProId", createOrderRequest.getProId());
        params.put("OrderAdults", createOrderRequest.getOrderAdults().toString());
        params.put("BookCount", createOrderRequest.getBookCount().toString());
        params.put("OrderUseDateDetails",
                new ObjectMapper().writeValueAsString(createOrderRequest.getOrderUseDateDetails()));
        params.put("OrderPassengerDetails",
                new ObjectMapper().writeValueAsString(createOrderRequest.getOrderPassengerDetails()));
        params.put("ProductUniqueId", createOrderRequest.getProductUniqueId().toString());
        params.put("LinkManName", createOrderRequest.getLinkManName());
        params.put("LinkManSex", createOrderRequest.getLinkManSex().toString());
        params.put("LinkManMobile", createOrderRequest.getLinkManMobile());
        params.put("ArrivalTime", createOrderRequest.getArrivalTime().toString());
        params.put("PaymentSign", createOrderRequest.getPaymentSign().toString());

        responseJson.setObj(tongchengApiService.createSingleOrder(params));

        return responseJson;
    }

    @RequestMapping("/hotel/order/pay")
    public ResponseJson payOrder(@RequestBody PayOrderRequest payOrderRequest) throws Exception {
        ResponseJson responseJson = new ResponseJson();
        responseJson.setObj(tongchengApiService.payOrder(payOrderRequest.getOrderId(),
                payOrderRequest.getPayMoney()));
        return responseJson;
    }

    @RequestMapping("/hotel/order/list")
    public ResponseJson orderList(@RequestParam Map<String, String> params) throws Exception {
        ResponseJson responseJson = new ResponseJson();
        responseJson.setObj(tongchengApiService.getOrderList(params));
        return responseJson;
    }
}
